<template>
	<view class="uni-container">
		<view class="uni-container-table">
			<uni-table ref="table" :loading="loading" border stripe type="selection" emptyText="暂无更多数据"
				@selection-change="selectionChange">
				<uni-tr>
					<uni-th :width="30" align="center">id</uni-th>
					<uni-th :width="40" align="center">ip</uni-th>
					<uni-th :width="50" align="center">设置</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td align="center">{{ item.id }}</uni-td>
					<uni-td align="center">
						<view class="name">{{ item.ip }}</view>
					</uni-td>
					<uni-td align="center">
						<view class="uni-group">
							<button class="uni-button" @click="showDrawer('showLeft',item)" size="mini"
								type="primary">修改</button>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
			<view class="uni-pagination-box">
				<uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total"
					@change="change" />
			</view>
		</view>

		<view class="info-set">
			<uni-drawer ref="showLeft" mode="left" :width="320" :mask-click="false">
				<view class="uni-navbar">
					<view>
						<uni-segmented-control :current="current" :values="items" :style-type="styleType"
							:active-color="activeColor" @clickItem="onClickItem" />
					</view>
					<view class="content" style="margin-top: 10rpx;">
						<view class="content">
							<view v-if="current === 0">
								<uni-section v-if="current === 0" :title="id + '号主机运行参数'" type="line">
									<uni-card :is-shadow="false">
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">输出频率</uni-col>
											<uni-col style=" text-align: center; color: green;"
												:span="8">{{this.rumParam.vd400}}</uni-col>
											<uni-col style="text-align: left;font-size:40rpx;" :span="8">Hz</uni-col>
										</uni-row>
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">输出电流</uni-col>
											<uni-col style=" text-align: center; color: green;	"
												:span="8">{{this.rumParam.vd404}}</uni-col>
											<uni-col style="text-align: left;" :span="8">A</uni-col>
										</uni-row>
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">输出电压</uni-col>
											<uni-col style=" text-align: center; color: green;	"
												:span="8">{{this.rumParam.vd408}}</uni-col>
											<uni-col style="text-align: left;" :span="8">V</uni-col>
										</uni-row>
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">马达转速</uni-col>
											<uni-col style=" text-align: center; color: green;	"
												:span="8">{{this.rumParam.vw118}}</uni-col>
											<uni-col style="text-align: left;" :span="8">RPM</uni-col>
										</uni-row>
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">输出转矩</uni-col>
											<uni-col style=" text-align: center; color: green;	"
												:span="8">{{this.rumParam.vd412}}</uni-col>
											<uni-col style="text-align: left;" :span="8">%</uni-col>
										</uni-row>
										<uni-row
											style="border-bottom: 1rpx gray solid;font-size:40rpx;margin-bottom: 10rpx;">
											<uni-col style=" text-align: right;" :span="8">输出功率</uni-col>
											<uni-col style=" text-align: center; color: green;font-size:40rpx;	"
												:span="8">{{this.rumParam.vd416}}</uni-col>
											<uni-col style="text-align: left;" :span="8">kw</uni-col>
										</uni-row>
									</uni-card>
								</uni-section>
							</view>
							<view v-if="current === 1">
								<uni-section :title="id + '号主机循环参数设置'" type="line">
									<uni-card :is-shadow="false">
										<uni-row style="margin-bottom: 10rpx;border-bottom: 1rpx gray solid;"
											v-for="(item,index) in weekList" :key="index">
											<uni-col
												style="height: 70rpx;display: flex; align-items: center;justify-content: center;"
												:span="8">星期{{item}}</uni-col>
											<uni-col style="display: flex; align-items: center;justify-content: right;"
												:span="8" :offset="4">
												<switch :checked="repeatWeek[index]"
													@change="e => weekCheckChange(e,index)" />
											</uni-col>
										</uni-row>
									</uni-card>
								</uni-section>

							</view>
							<view v-if="current === 2">
								<uni-section :title="id + '号主机启停时间设置'" type="line" style="width: 100%;">
									<uni-card :is-shadow="false">
										<uni-row class="row-repeat-time">
											<uni-col :span="4">序号</uni-col>
											<uni-col :span="7">开始时间</uni-col>
											<uni-col :span="7">结束时间</uni-col>
											<uni-col :span="6">
												是否开启
											</uni-col>
										</uni-row>
										<uni-row v-for="(item,index) in repeatTime" :key="index"
											class="row-repeat-time">
											<uni-col :span="4">{{index + 1}}</uni-col>
											<uni-col :span="7">
												<picker mode="time" :value="item.startTime"
													@change="e =>bindStartTimeChange(e,index)">
													<view class="uni-input">{{item.startTime}}</view>
												</picker>
											</uni-col>
											<uni-col :span="7">
												<picker mode="time" :value="item.endTime"
													@change="e =>bindEndTimeChange(e,index)">
													<view class="uni-input">{{item.endTime}}</view>
												</picker>
											</uni-col>
											<uni-col :span="6">
												<switch :checked="item.checked"
													@change="e =>repeatTimeChange(e,index)" />
											</uni-col>
										</uni-row>
									</uni-card>
									<uni-card>
										<uni-row>
											<uni-col :offset="4" :span="8">运行时间</uni-col>
											<uni-col :span="8">
												<picker mode="time" :start="repeatRunTime.start"
													:end="repeatRunTime.end" @change="bindRunTime">
													<view class="uni-input">{{repeatRunTime.runTime}}</view>
												</picker>
											</uni-col>
										</uni-row>
										<uni-row>
											<uni-col :offset="4" :span="8">停止时间</uni-col>
											<uni-col :span="8">
												<picker mode="time" :start="repeatRunTime.start"
													:end="repeatRunTime.end" @change="bindStopTime">
													<view class="uni-input">{{repeatRunTime.stopTime}}</view>
												</picker>
											</uni-col>
										</uni-row>

									</uni-card>
								</uni-section>
							</view>
						</view>
					</view>


					<view class="close" v-if="current != 0">
						<button class="uni-button" type="primary" size="mini"
							@click="closeDrawer('showLeft')">取消</button>
						<button class="uni-button" type="primary" size="mini"
							@click="writeRepeatParams('showLeft')">确定</button>
					</view>
					<view class="close" v-else>
						<button class="uni-button" type="primary" size="mini"
							@click="closeDrawer('showLeft')">返回</button>
					</view>
				</view>
			</uni-drawer>
		</view>

	</view>
</template>


<script>
	import api from "../../api/api.js"
	export default {
		data() {
			return {
				tableData: [],
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				hostIdList: [],
				phone: '',

				showRight: false,
				showLeft: false,

				// 抽屉
				items: ['查看运行参数', '循环日期设置', '循环时间设置'],
				current: 0,
				colors: ['#007aff', '#4cd964', '#dd524d'],
				activeColor: '#007aff',
				styleType: 'button',
				rumParam: {
					vw118: 0,
					vd400: 0.0,
					vd404: 0.0,
					vd408: 0.0,
					vd412: 0.0,
					vd416: 0.0
				},
				id: 0,
				ip: '',
				weekList: ['日', '一', '二', '三', '四', '五', '六'],
				repeatWeek: [false, false, false,
					false, false, false, false
				],
				// 运行时间段
				repeatTime: [{
						startTime: '00:00',
						endTime: '12:12',
						checked: false
					},
					{
						startTime: '00:00',
						endTime: '12:12',
						checked: false
					},
					{
						startTime: '00:00',
						endTime: '12:12',
						checked: false
					},
					{
						startTime: '00:00',
						endTime: '12:12',
						checked: false
					},
				],
				// 自动运行停止时间
				repeatRunTime: {
					runTime: '00:00',
					stopTime: '00:00',
					start: '00:00',
					end: '05:00'
				},
				repeatParams: {
					id: 0,
					// 周日--周六开启状态
					v5100: false,
					v5101: false,
					v5102: false,
					v5103: false,
					v5104: false,
					v5105: false,
					v5106: false,

					vb500_0: false, // 自动模式下运行持续时间-分钟
					vb501_0: false, // 自动模式下运行持续时间-秒
					vb502_0: false, // 自动模式下停止持续时间-分钟
					vb503_0: false, // 自动模式下停止持续时间-秒

					v5110: false, //循环1:是否开启标记
					v5111: false, //循环2:是否开启标记
					v5112: false, //循环3:是否开启标记
					v5113: false, //循环4:是否开启标记
					vw512: 0, //循环1：开始小时
					vw514: 0, //循环1：结束小时
					vw516: 0, //循环2：开始小时
					vw518: 0, //循环2：结束小时
					vw520: 0, //循环3：开始小时
					vw522: 0, //循环3：结束小时
					vw524: 0, //循环4：开始小时
					vw526: 0, //循环4：结束小时
					vw528: 0, //循环1：开始分钟
					vw530: 0, //循环1：结束分钟
					vw532: 0, //循环2：开始分钟
					vw534: 0, //循环2：结束分钟
					vw536: 0, //循环3：开始分钟
					vw538: 0, //循环3：结束分钟
					vw540: 0, //循环4：开始分钟
					vw542: 0, //循环4：结束分钟
				},
				time: '00:00:12'
			}
		},
		onLoad() {
			this.selectedIndexs = [];
			// this.getData(1);
			uni.getStorage({
				key: api.base.userInfo,
				success: res => {
					// console.log("读取本地缓存：",res);
					this.phone = res.data.phone;
				}
			})
			setTimeout(() => {
				this.getData(this.pageCurrent)
			}, 500)

		},
		onPullDownRefresh() {
			// console.log("触发下拉了")
			this.getData(this.pageCurrent)
			uni.stopPullDownRefresh();
		},
		methods: {

			findByPage(current, size) {
				api.findByPage({
					current: current,
					size: size,
					phone: this.phone
				}).then(res => {
					// console.log("查询结果：", res);
					if (res.code === 200) {
						this.tableData = res.data.records;
						this.total = res.data.total;
						// console.log(this.tableData)
					} else {
						uni.showToast({
							icon: "none",
							title: res.msg,
							mask: true,
							duration: 1000
						})
					}
					this.loading = false;
				})
			},
			// 多选
			selectionChange(e) {
				console.log(e.detail.index)
				this.selectedIndexs = e.detail.index
				this.hostIdList = [];
				this.selectedIndexs.map(i => this.hostIdList.push(this.tableData[i]))
				console.log(this.hostIdList)
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection();
				this.selectedIndexs.length = 0;
				this.pageCurrent = e.current;
				// console.log("当前页面：", e.current, this.pageCurrent);
				this.getData(this.pageCurrent);
				uni.stopPullDownRefresh();
			},
			// 获取数据
			getData(pageCurrent) {
				this.loading = true
				this.pageCurrent = pageCurrent
				this.findByPage(this.pageCurrent, this.pageSize);
			},


			//======================打开关闭抽屉==========================//
			// 打开窗口
			showDrawer(e, host) {
				console.log(e);
				this.id = host.id;
				this.ip = host.ip;
				api.readRunParamById({
					id: this.id
				}).then(res => {
					if (res.code === 200) {
						this.rumParam = res.data;
						// console.log("参数：", res.data);
					} else {
						// console.log("错误信息：", res.msg);
					}
				})
				api.readRepeatParamById({id:this.id}).then(res =>{
					if(res.code === 200){
						// console.log("读取plc返回参数：",res);
						this.repeatParams = res.data;
						this.toViewParam();
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg
						})
					}
				})
				this.$refs[e].open()
			},
			// 关闭窗口
			closeDrawer(e) {
				console.log(e);
				this.$refs[e].close()
			},

			writeRepeatParams(e) {
				this.toRequestParam();
				api.writeRepeatParamByJson(this.repeatParams).then(res => {
					if (res.code === 200) {
						this.closeDrawer(e);
					}
					uni.showToast({
						icon: 'none',
						title: res.msg,
						mask: true
					});
				});

			},
			// 抽屉状态发生变化触发
			// change(e, type) {
			// 	console.log("change函数：", e, type);
			// 	console.log((type === 'showLeft' ? '左窗口' : '右窗口') + (e ? '打开' : '关闭'));
			// 	this[type] = e
			// },
			repeatTimeChange(e, index) {
				this.repeatTime[index].checked = e.detail.value;
				// console.log("repeatTimeChange:",e.detail.value,index);
			},

			//================抽屉内容========================//
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
			},
			weekCheckChange(e, index) {
				// console.log('switch开关切换了', e.detail.value, index);
				this.repeatWeek[index] = e.detail.value;
				// console.log(this.repeatWeek);
			},

			bindStartTimeChange(e, index) {
				this.repeatTime[index].startTime = e.detail.value;
				// console.log("开始时间：", e, this.repeatTime);
			},

			bindEndTimeChange(e, index) {
				this.repeatTime[index].endTime = e.detail.value;
				// console.log("结束时间：", e, this.repeatTime);
			},
			// 自动运行时，自动时间和停止时间
			bindRunTime(e) {
				// console.log("运行时间：",e);
				this.repeatRunTime.runTime = e.detail.value;
				let ms = this.repeatRunTime.runTime.split(":")
				this.repeatParams.vb500_0 = ms[0];
				this.repeatParams.vb501_0 = ms[1];

			},
			bindStopTime(e) {
				// console.log("停止时间：",e);
				this.repeatRunTime.stopTime = e.detail.value;
				let ms = this.repeatRunTime.stopTime.split(":");
				this.repeatParams.vb502_0 = ms[0];
				this.repeatParams.vb503_0 = ms[1];
				// console.log(this.repeatParams);
			},
			// 封装写入plc参数
			toRequestParam() {
				let arr0 = this.repeatTime[0].startTime.split(":");
				this.repeatParams.vw512 = arr0[0];
				this.repeatParams.vw528 = arr0[1];
				let arr1 = this.repeatTime[1].startTime.split(":");
				this.repeatParams.vw516 = arr1[0];
				this.repeatParams.vw532 = arr1[1];
				let arr2 = this.repeatTime[2].startTime.split(":");
				this.repeatParams.vw520 = arr2[0];
				this.repeatParams.vw536 = arr2[1];
				let arr3 = this.repeatTime[3].startTime.split(":");
				this.repeatParams.vw524 = arr3[0];
				this.repeatParams.vw540 = arr3[1];

				let brr0 = this.repeatTime[0].endTime.split(":");
				this.repeatParams.vw514 = brr0[0];
				this.repeatParams.vw530 = brr0[1];
				let brr1 = this.repeatTime[1].endTime.split(":");
				this.repeatParams.vw518 = brr1[0];
				this.repeatParams.vw534 = brr1[1];
				let brr2 = this.repeatTime[2].endTime.split(":");
				this.repeatParams.vw522 = brr2[0];
				this.repeatParams.vw538 = brr2[1];
				let brr3 = this.repeatTime[3].endTime.split(":");
				this.repeatParams.vw526 = brr3[0];
				this.repeatParams.vw542 = brr3[1];
				this.repeatParams.v5110 = this.repeatTime[0].checked;
				this.repeatParams.v5111 = this.repeatTime[1].checked;
				this.repeatParams.v5112 = this.repeatTime[2].checked;
				this.repeatParams.v5113 = this.repeatTime[3].checked;

				this.repeatParams.v5100 = this.repeatWeek[0];
				this.repeatParams.v5101 = this.repeatWeek[1];
				this.repeatParams.v5102 = this.repeatWeek[2];
				this.repeatParams.v5103 = this.repeatWeek[3];
				this.repeatParams.v5104 = this.repeatWeek[4];
				this.repeatParams.v5105 = this.repeatWeek[5];
				this.repeatParams.v5106 = this.repeatWeek[6];

				this.repeatParams.id = this.id;

			},
			// 回显plc参数
			toViewParam() {
				// let arr0 = this.repeatTime[0].startTime.split(":");

				this.repeatTime[0].startTime = this.toFromatterStr(this.repeatParams.vw512) + ":" + this.toFromatterStr(this.repeatParams.vw528);
				this.repeatTime[1].startTime = this.toFromatterStr(this.repeatParams.vw516) + ":" +this.toFromatterStr(this.repeatParams.vw532);
				this.repeatTime[2].startTime = this.toFromatterStr(this.repeatParams.vw520) + ":" +this.toFromatterStr(this.repeatParams.vw536);
				this.repeatTime[3].startTime = this.toFromatterStr(this.repeatParams.vw524) + ":" +this.toFromatterStr(this.repeatParams.vw540);

				this.repeatTime[0].endTime = this.toFromatterStr(this.repeatParams.vw514) + ":" + this.toFromatterStr(this.repeatParams.vw530);
				this.repeatTime[1].endTime = this.toFromatterStr(this.repeatParams.vw518) + ":" + this.toFromatterStr(this.repeatParams.vw534);
				this.repeatTime[2].endTime = this.toFromatterStr(this.repeatParams.vw522) + ":" + this.toFromatterStr(this.repeatParams.vw538);
				this.repeatTime[3].endTime = this.toFromatterStr(this.repeatParams.vw526) + ":" + this.toFromatterStr(this.repeatParams.vw542);
				this.repeatTime[0].checked = this.repeatParams.v5110;
				this.repeatTime[1].checked = this.repeatParams.v5111;
				this.repeatTime[2].checked = this.repeatParams.v5112;
				this.repeatTime[3].checked = this.repeatParams.v5113;

				this.repeatWeek[0] = this.repeatParams.v5100;
				this.repeatWeek[1] = this.repeatParams.v5101;
				this.repeatWeek[2] = this.repeatParams.v5102;
				this.repeatWeek[3] = this.repeatParams.v5103;
				this.repeatWeek[4] = this.repeatParams.v5104;
				this.repeatWeek[5] = this.repeatParams.v5105;
				this.repeatWeek[6] = this.repeatParams.v5106;

				this.repeatRunTime.runTime = this.toFromatterStr(this.repeatParams.vb500_0) + ":" + this.toFromatterStr(this.repeatParams.vb501_0);
				this.repeatRunTime.stopTime = this.toFromatterStr(this.repeatParams.vb502_0) + ":" + this.toFromatterStr(this.repeatParams.vb503_0);

			},
			//时间格式化 
			toFromatterStr(strNum){
				if(parseInt(strNum) <= 9){
					return "0" + strNum;
				}
				return strNum;
			},

		}
	}
</script>


<style lang="scss">
	.uni-container-table {}

	.uni-group {
		display: flex;
		align-items: center;
	}


	.scroll-view {
		/* #ifndef APP-NVUE */
		width: 100%;
		height: 100%;
		/* #endif */
		flex: 1;
		width: 100%;
		height: 100%;
	}

	// .uni-common-mt{
	// 	position: fixed;
	// 	top: 10rpx; 
	// 	justify-content: center;
	// 	align-items: center;
	// 	width: 80%;
	// 	height: 40rpx;
	// }

	// 处理抽屉内容滚动
	// .scroll-view-box {
	// 	flex: 1;
	// 	position: absolute;
	// 	top: 0;
	// 	right: 0;
	// 	bottom: 0;
	// 	left: 0;
	// }

	// .info {
	// 	padding: 15px;
	// 	color: #666;
	// }

	// .info-text {
	// 	font-size: 14px;
	// 	color: #666;
	// }
	// .info-content {
	// 	padding: 5px 15px;
	// }
	.close {
		// padding: 10px;
		display: flex;
		// flex-direction: column;
		justify-content: center;
		align-items: center;

	}

	.row-repeat-time {
		margin-bottom: 10rpx;
		border-bottom: 1rpx gray solid;
		padding: 10rpx;

		.uni-col {
			// border: 1rpx red solid;
			height: 70rpx;
			// text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>